﻿using System;
using System.Collections.Generic;
using System.Linq;
using Model.Entity;
using Ones.Dal.Repository.Impl;
using Ones.Infrastructure.Utils.Pagination.Impl;

namespace Ones.Wcf.SystemAdministration
{
    public class ServiceHumanResourceManagement : IServiceHumanResourceManagement
    {
        private IDivisionDao _daoDiv;
        private IJobDao _daoJob;
        private IEmployeeDao _daoEmp;
        private IJobAssignmentDao _daoJA;
        private IViewEmployeeDetailDao _daoEmpDetail;

        #region Constructor
 
        public ServiceHumanResourceManagement()
        {
            _daoDiv = new DivisionDao();
            _daoJob = new JobDao();
            _daoEmp = new EmployeeDao();
            _daoJA = new JobAssignmentDao();
            _daoEmpDetail = new ViewEmployeeDetailDao();
        }

        #endregion

        #region Div
        
        #region DMM
        
        public bool
        addDivision(Division division)
        {
            return saveDiv(division, true);
        }
        
        public bool
        updateDivision(Division division)
        {
            return saveDiv(division, false);
        }
        
        public bool
        deleteDivision(string id)
        {
            return _daoDiv.delete(id);
        }

        #endregion

        #region DQM
        
        public List<Division>
        searchAllDivisions()
        {
            return _daoDiv.getAll();
        }
        
        public Division
        searchDivisionById(string id)
        {
            return _daoDiv.getById(id);
        }
        
        public List<Division>
        searchDivisionByName(string name)
        {
            return _daoDiv.getByName(name);
        }

        
        //cua V
        public List<JobAssignment> searchDivisionByEmpId(string empID)
        {
            try 
            {
                return _daoJA.getByEmployee(empID);
            }
            catch (Exception)
            {
                return null;
            }
        }

        #endregion
        
        #endregion Div
        
        #region Job
        
        #region DMM

        public bool
        addJob(Job job)
        {
            return saveJob(job, true);
        }
        
        public bool
        updateJob(Job job)
        {
            return saveJob(job, false);
        }
        
        public bool
        deleteJob(string id)
        {
            return _daoJob.delete(id);
        }
        
        #endregion
        
        #region DQM
        
        public List<Job>
        searchAllJobs()
        {
            return _daoJob.getAll();
        }
        
        public Job
        searchJobById(string id)
        {
            return _daoJob.getById(id);
        }
        
        public List<Job>
        searchJobByTitle(string title)
        {
            return _daoJob.getByTitle(title);
        }
    
        #endregion

        #endregion Job

        #region Emp
        
        #region DMM
        
        //this method should be in transaction.
        public bool
        enrollEmployee(Employee employee, string divId, string jobId, DateTime dateStart, DateTime dateFinish)
        {
            try
            {
                bool res1 = _daoEmp.create(employee);
                
                JobAssignment ja = new JobAssignment();
                ja.empId = employee.empId;
                ja.divId = divId;
                ja.jobId = jobId;
                ja.jaDateModify = DateTime.Today;
                ja.jaDateStart = dateStart;
                ja.jaDateFinish = dateFinish;
                
                bool res2 = _daoJA.create(ja);
                
                return (res1 && res2);
            }
            catch (Exception)
            {
                return false;
            }
        }
        
        public bool
        updateEmployeeInfo(Employee employee)
        {
            employee.empDateModify = DateTime.Today;
            
            return _daoEmp.update(employee);
        }
        
        public bool
        updateEmployeeDivision(string empId, string divId)
        {
            // TODO: Implement this method
            throw new NotImplementedException();
        }
        
        public bool
        updateEmployeeJob(string empId, string jobId)
        {
            // TODO: Implement this method
            throw new NotImplementedException();
        }
        
        #endregion
        
        #region DQM
        
        public List<Employee>
        searchAllEmployees()
        {
            return _daoEmp.getAll();
        }
        
        public Employee
        searchEmployeeById(string empId)
        {
            return _daoEmp.getById(empId);
        }
        
        public Employee
        searchEmployeeByUser(Guid userId)
        {
            return _daoEmp.getByUser(userId);
        }
        
        public List<Employee>
        searchEmployeeByName(string name)
        {
            return _daoEmp.getByName(name);
        }
        
        public List<Employee>
        searchEmployeeByGender(bool isMale)
        {
            return _daoEmp.getByGender(isMale);
        }
        
        #endregion
        
        #endregion Emp
        
        #region View Emp Detail
        
        public List<ViewEmployeeDetail>
        searchAllEmployeeDetails()
        {
            return _daoEmpDetail.getAll();
        }
        
        #endregion View Emp Detail
        
        #region ///Paging///
        
        public LazyPagination<Division>
        pagingAllDivisions(int pageNumber, int pageSize)
        {
            try
            {
                var lst = _daoDiv.getAll().AsQueryable();
                return new LazyPagination<Division>(lst, pageNumber, pageSize);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public LazyPagination<Job>
        pagingAllJobs(int pageNumber, int pageSize)
        {
            try
            {
                var lst = _daoJob.getAll().AsQueryable();
                return new LazyPagination<Job>(lst, pageNumber, pageSize);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public LazyPagination<Employee>
        pagingAllEmployees(int pageNumber, int pageSize)
        {
            try
            {
                var lst = _daoEmp.getAll().AsQueryable();
                return new LazyPagination<Employee>(lst, pageNumber, pageSize);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public LazyPagination<ViewEmployeeDetail>
        pagingAllEmployeeDetails(int pageNumber, int pageSize)
        {
            try
            {
                var lst = _daoEmpDetail.getAll().AsQueryable();
                return new LazyPagination<ViewEmployeeDetail>(lst, pageNumber, pageSize);
            }
            catch (Exception)
            {
                return null;
            }
        }
        
        #endregion
        
        private bool
        saveDiv(Division div, bool isCreate)
        {
            try
            {
                if (isCreate)
                {
                    return _daoDiv.create(div);
                }
                return _daoDiv.update(div);
            }
            catch (Exception)
            {
                return false;
            }
        }
        
        private bool
        saveJob(Job job, bool isCreate)
        {
            try
            {
                if (isCreate)
                {
                    return _daoJob.create(job);
                }
                return _daoJob.update(job);
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
//try
//{
//    return true;
//}
//catch (Exception ex)
//{
//    return false;
//}